﻿using System;
using System.Collections.Generic;
using System.Text;
using Gammar.Digger.Entity;
using Gammar.Digger.IDAL;
using Gammar.Digger.DALFactory;
using Gammar.Digger.SystemFramework;
using System.Threading;
using System.Xml;
using System.Data;
namespace Gammar.Digger.BLL
{
    public class DuplicateService
    {
        public DuplicateService()
        {
            AppUtility.WriteAccess("start......");

            List<string[]> tables = GetTableNames();

            IDbHelper helper = DataAccess.CreateDbHelper(ConfigUtility.GetConnectionString("conn_SqlServer_GPriceCollect_Data"));

            int success = 0;
            int failed = 0;
            int noneed = 0;
            int total=0;
            foreach (string[] strs in tables)
            {
                total++;

                try
                {
                    DataTable dt = helper.GetDuplicateData(strs[0]);

                    

                    if (dt != null )
                    {

                        if (dt.Rows.Count > 0)
                        {
                            AppUtility.WriteAccess(strs[0] + "\t total number" + dt.Rows.Count);

                            //delete url duplicate data
                            helper.DeleteDuplicateData(strs[0]);
                            AppUtility.WriteAccess(string.Format("clear {0}    OK!", strs[0]));

                            string ids = string.Empty;

                            foreach (DataRow row in dt.Rows)
                            {
                                ids = ids == string.Empty ? "'" + row[0].ToString() + "'" : ids + "," + "'" + row[0].ToString() + "'";
                            }

                            //delete data
                            helper.ExecuteNonQuery(@"delete from " + strs[1] + " where url_id in (" + ids + ")");

                            AppUtility.WriteAccess(string.Format("clear {0}    OK!", strs[1]));

                            success++;
                        }
                        else
                        {
                            noneed++;
                        }
                      
                    }
                    else
                    {
                        AppUtility.WriteError("Get " + strs[0]+"\tFailed");
                    }
                   

                }
                catch (Exception ex)
                {

                    AppUtility.WriteError(ex.Message);
                    failed++;
                }         

            }

            AppUtility.WriteAccess(string.Format("total:{0}\t success:{1}\t failed:{2}\t noneed:{3}",total,success,failed,noneed));
        }

        public List<string[]> GetTableNames()
        {
            List<string[]> reval = new List<string[]>();
            Iweb_site website = DataAccess.Createweb_site();
            SortList<web_siteEntity> webList = website.GetList("");

            Idigger_plugin plugin = DataAccess.Createdigger_plugin();
            foreach (web_siteEntity w in webList)
            {
                SortList<digger_pluginEntity> pluginlist = plugin.GetList("site_id=" + w.site_id);

                foreach (digger_pluginEntity p in pluginlist)
                {
                    string prefix = "tb_" + w.site_id.ToString() + "_" + p.plugin_id.ToString();
                    string[] value = new string[] { prefix + "_url", prefix + "_data", prefix + "_comments" };
                    reval.Add(value);
                }
            }

            return reval;
        }

        
    }
}
